/**
 * 视频设备配置（添加|修改）videoProject.js
 * 版本号： v1.0.0
 */
$(function(){
    //设置输入框的最大输入长度
    $(".easyui-textbox").each(function(){
        var data_options = $(this).attr("data-options");
        if(data_options){
            var matchs = data_options.match(/length\[1,(\d*)\]/);
            if(matchs){
                $(this).textbox("textbox").attr("maxlength", matchs[1] - 0 + 1);
            }else{
                $(this).textbox("textbox").attr("maxlength", 20);
            }
        }else{
            $(this).textbox("textbox").attr("maxlength", 20);
        }
    });

    //所属项目绑定
    bindCombo_projectSel("prId");

    //所属项目绑定
    bindCombo_hkUserSel("userUuid");

    //操作按钮点击
    $('#optionVideoProject').bind('click', function(){
        var params = $("#videoProject_form").serialize();
        $("#videoProject_form").form("submit",{
            cache:false,
            data: params,
            url: CONTEXT_PATH + "/videoProject/save.do",
            onSubmit:function() {
                preventSubmit('videoProject_form','optionVideoProject');
                return $(this).form("validate");
            },
            success:function(data) {
                var obj = JSON.parse(data);
                var url = null;
                if(source == "deviceManage"){
                    url = CONTEXT_PATH + "/device/index.do";
                }else{
                    url = CONTEXT_PATH + "/videoProject/index.do";
                }
                saveJump(obj,url);
            }
        });
    });

});

/**
 * 所属项目bind事件
 * @param deviceControl
 */
function bindCombo_projectSel(projectControl){
    $.ajax({
        url: CONTEXT_PATH + "/project/getProjectListForCombo.do",
        type: "get",
        success: function (data) {
            var obj = JSON.parse(data);
            var projectCombo = [{'text': '--请选择--','value': ''}];
            for (var i = 0; i < obj.length; i++) {
                projectCombo.push({"text": obj[i].prName, "value": obj[i].prId});
            }
            $("#" + projectControl).combobox("loadData", projectCombo);
            $("#" + projectControl).combobox({
                required:true,
                onSelect: function(rec){
                    $('#prId_').val(rec.value);
                }
            });
            var prId = $('#prId_').val();
            if(prId){
                $("#" + projectControl).combobox("setValue", prId);
            }else{
                $("#" + projectControl).combobox("setValue", '');
            }

        }
    })
}

function bindCombo_hkUserSel(userController){
    var userUuid = $('#userUuid_').val();
    $.ajax({
        url: CONTEXT_PATH + "/hkUser/getHkUserCombo.do?userUuid="+userUuid,
        type: "get",
        success: function (data) {
            var obj = JSON.parse(data);
            var hkUserCombo = [{'text': '--请选择--','value': ''}];
            for (var i = 0; i < obj.length; i++) {
                hkUserCombo.push({"text": obj[i].userName, "value": obj[i].userUuid});
            }
            $("#" + userController).combobox("loadData", hkUserCombo);
            $("#" + userController).combobox({
                required:true,
                onSelect: function(rec){
                    $('#userUuid_').val(rec.value);
                }
            });
            if(userUuid != "" && userUuid != null){
                $("#" + userController).combobox("setValue", userUuid);
            }else{
                $("#" + userController).combobox("setValue", '');
            }

        }
    })
}

